package com.zh.work.controller.platform.trans;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.common.entity.JSONResult;
import com.zh.work.entity.trans.QrCodeOrderHistoryEntity;
import com.zh.work.entity.trans.QrCodeOrderEntity;
import com.zh.work.service.platform.trans.QrCodeOrderService;
import com.zh.work.util.plateform.QrCodeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.*;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 二维码订单交易控制器
 * @Author: Li.HuiNeng
 * @Create in: 2019-09-09 16:31
 * @Version: 1.0.1
 */
@Api(value = "二维码订单交易控制器", produces = "application/json", tags = {"二维码订单交易控制器"})
@RestController
@RequestMapping("/qrCodeOrder")
public class QrCodeOrderController {
    private static Logger log = LoggerFactory.getLogger(QrCodeOrderController.class);

//    @Autowired
//    private AmqpTemplate amqpTemplate;

    @Autowired
    private QrCodeOrderService service;


    @ApiOperation(value = "分页查询二维码实时数据集", tags = {"getPagesQrCodeOrders"}, notes = "分页查询二维码实时数据集")
    @GetMapping("/getPagesQrCodeOrders")
    public JSONResult<List<QrCodeOrderEntity>> getPagesQrCodeOrders(
            @ApiParam(value = "起始页", name = "pageNo") Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageNo") Integer pageSize,
            @ApiParam(value = "二维码订单实体类", name = "qrcodeOrderEntity") QrCodeOrderEntity qrcodeOrderEntity) {
        JSONResult<List<QrCodeOrderEntity>> jsonResult = new JSONResult<>();
        try {
            IPage<QrCodeOrderEntity> page = new Page<>(pageNo, pageSize);
            IPage<QrCodeOrderEntity> result = this.service.selectQrCodeOrderPages(page, qrcodeOrderEntity);
            jsonResult.setTotal(page.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "分页查询历史订单数据集", tags = {"getPagesQrCodeOrderHistory"}, notes = "分页查询历史订单数据集")
    @GetMapping("/getPagesQrCodeOrderHistory")
    public JSONResult<List<QrCodeOrderHistoryEntity>> getPagesQrCodeOrderHistory(
            @ApiParam(value = "起始页", name = "pageNo") Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageNo") Integer pageSize,
            @ApiParam(value = "二维码历史订单实体类", name = "qrCodeOrderHistoryEntity") QrCodeOrderHistoryEntity qrCodeOrderHistoryEntity) {
        JSONResult<List<QrCodeOrderHistoryEntity>> jsonResult = new JSONResult<>();
        try {
            IPage<QrCodeOrderHistoryEntity> page = new Page<>(pageNo, pageSize);
            IPage<QrCodeOrderHistoryEntity> result = this.service.selectOrderHistoryPages(page, qrCodeOrderHistoryEntity);
            jsonResult.setTotal(page.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "查询二维码订单统计数据", tags = {"getPagesQrCodeOrderHistory"}, notes = "查询二维码订单统计数据")
    @GetMapping("/getQrCodeStatistic")
    public JSONResult<List<Map<String, String>>> getQrCodeStatistic(@ApiParam(value = "二维码订单实体类", name = "qrcodeOrderEntity")
                                                                                QrCodeOrderEntity qrcodeOrderEntity) {
        JSONResult<List<Map<String, String>>> jsonResult = new JSONResult<>();
        List<Map<String, String>> list = null;
/*        if (Objects.isNull(qrcodeOrderEntity.getStartDateTime())) {
            qrcodeOrderEntity.setStartDateTime(DateSetUtil.getDayFirstSec(new Date()));
        }
        if (Objects.isNull(qrcodeOrderEntity.getEndDateTime())) {
            qrcodeOrderEntity.setEndDateTime(DateSetUtil.getDayLastSec(new Date()));
        }*/
        try {
            list = this.service.getQrCodeStatistic(qrcodeOrderEntity);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
        }
        if (list != null) {
            if (list.size() > 0) {
                jsonResult.setSuccess(true);
                jsonResult.setData(list);
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("统计数量为0");
            }
        } else {
            jsonResult.setSuccess(false);
            jsonResult.setMessage("统计数量为空");
        }
        return jsonResult;
    }

    /*@PostMapping("/encodeQRCode")
    public String encodeQRCode() {
        log.info("生成二维码测试!");
        try {
            QrCodeOrderHistoryEntity qrCodeOrderHistoryEntity = new QrCodeOrderHistoryEntity();
            qrCodeOrderHistoryEntity.setId(1L);
            qrCodeOrderHistoryEntity.setAccountName("12233");
            qrCodeOrderHistoryEntity.setAccountNo("aaaaa");
            qrCodeOrderHistoryEntity.setAmount(1.2F);
            qrCodeOrderHistoryEntity.setBankName("sasdc");
            QrCodeUtil.encodeQRCode(JSONObject.toJSONString(qrCodeOrderHistoryEntity), "./first.png");
        } catch (Exception e) {
            e.printStackTrace();
            return e.getLocalizedMessage();
        }
        return "生成完成";
    }


    @PostMapping("/decodeQRCodeBase64")
    public String encodeQRCodeBase64() {
        log.info("生成Bsse64二维码测试!");
        String base64 = "";
        QrCodeOrderHistoryEntity qrCodeOrderHistoryEntity = new QrCodeOrderHistoryEntity();
        qrCodeOrderHistoryEntity.setId(1L);
        qrCodeOrderHistoryEntity.setAccountName("12233");
        qrCodeOrderHistoryEntity.setAccountNo("aaaaa");
        qrCodeOrderHistoryEntity.setAmount(1.2F);
        qrCodeOrderHistoryEntity.setBankName("sasdc");
        try {
            base64 = QrCodeUtil.encodeQRCodeBase64(JSONObject.toJSONString(qrCodeOrderHistoryEntity), 100, 100);
        } catch (IOException e) {
            log.error("生成二维码出现错误");
            return e.getLocalizedMessage();
        }
        return base64;
    }

    @PostMapping("/decodeQRCode")
    public JSONObject decodeQRCode() {
        log.info("解析二维码测试!");
        JSONObject jsonObject = QrCodeUtil.decodeQRCode("./first.png");
        return jsonObject;
    }*/


}
